import 'package:flutter/material.dart';
void func1(){
}
class GradientButton extends StatelessWidget {
  GradientButton({
    this.colors=const[],
    this.width=2,
    this.height=2,
    this.onPressed=func1,
    //this.borderRadius=BorderRadius.circular(1.0),
    this.child,
  });

  // 渐变色数组
  final List<Color> colors;

  // 按钮宽高
  final double width;
  final double height;

  Widget? child;
  BorderRadius borderRadius=BorderRadius.circular(1.0);

  //点击回调
  late GestureTapCallback onPressed;

  @override
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);

    //确保colors数组不空
    List<Color> _colors = colors ??
        [theme.primaryColor, theme.primaryColorDark ?? theme.primaryColor];
    return DecoratedBox(
          decoration: BoxDecoration(
            gradient: LinearGradient(colors: _colors),
            borderRadius: borderRadius,
            //border: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
          ),
          child: Material(
            type: MaterialType.transparency,
            child: InkWell(
              splashColor: _colors.last,
              highlightColor: Colors.transparent,
              //borderRadius: borderRadius,
              onTap: onPressed,
              child: ConstrainedBox(
                constraints: BoxConstraints.tightFor(height: height, width: width),
                child: Center(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: DefaultTextStyle(
                      style: TextStyle(fontWeight: FontWeight.bold),
                      child: child as Widget,
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
  }
}
